using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;


namespace MacroView.SharePointFields
{
    public class BDCField : SPFieldText
    {

        #region Constructors
        private static Dictionary<int, string> updatedApplicationProperty = new Dictionary<int, string>();
        private static Dictionary<int, string> updatedEntityProperty = new Dictionary<int, string>();
        private static Dictionary<int, string> updatedDisplayFieldsProperty = new Dictionary<int, string>();
        private static Dictionary<int, string> updatedReturnFieldProperty = new Dictionary<int, string>();

        public BDCField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {

            this.Init();

        }

        public BDCField(Microsoft.SharePoint.SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {

            this.Init();

        }

        #endregion

        private void Init()
        {
            try
            {
                this.Application = this.GetCustomProperty("Application") + "";
                this.Entity = this.GetCustomProperty("Entity") + "";
                this.DisplayFields = this.GetCustomProperty("DisplayFields") + "";
                this.ReturnField = this.GetCustomProperty("ReturnField") + "";
            }
            catch (Exception ex)
            { }
        }

        public void UpdateApplicationProperty(string value)
        {
            updatedApplicationProperty[ContextId] = value;
        }

        public void UpdateEntityProperty(string value)
        {
            updatedEntityProperty[ContextId] = value;
        }

        public void UpdateDisplayFieldsProperty(string value)
        {
            updatedDisplayFieldsProperty[ContextId] = value;
        }

        public void UpdateReturnFieldProperty(string value)
        {
            updatedReturnFieldProperty[ContextId] = value;
        }

        private string sApplication;
        private string sEntity;
        private string sDisplayFields;
        private string sReturnField;

        public string ReturnField
        {
            get
            {
                if (updatedReturnFieldProperty.ContainsKey(ContextId))
                {
                    return updatedReturnFieldProperty[ContextId];
                }
                else
                {
                    return sReturnField;
                }
            }
            set
            {
                this.sReturnField = value;
            }
        }

        public string DisplayFields
        {
            get
            {
                if (updatedDisplayFieldsProperty.ContainsKey(ContextId))
                {
                    return updatedDisplayFieldsProperty[ContextId];
                }
                else
                {
                    return sDisplayFields;
                }
            }
            set
            {
                this.sDisplayFields = value;
            }
        }

        public int ContextId
        {

            get
            {
                return SPContext.Current.GetHashCode();
            }

        }

        public string Application
        {
            get
            {
                //return this.sApplication;
                if (updatedApplicationProperty.ContainsKey(ContextId))
                {
                    return updatedApplicationProperty[ContextId];
                }
                else
                {
                    return sApplication;
                }

            }
            set
            {
                this.sApplication = value;
            }
        }

        public string Entity
        {
            get
            {
                if (updatedEntityProperty.ContainsKey(ContextId))
                {
                    return updatedEntityProperty[ContextId];
                }
                else
                {
                    return sEntity;
                }
            }
            set
            {
                this.sEntity = value;
            }
        }


        public override string GetValidatedString(object value)
        {

            string myVal = value as string;

            return myVal;

        }


        public override object GetFieldValue(string value)
        {

            if (String.IsNullOrEmpty(value))

                return null;


            return value;

        }



        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
        {

            get
            {

                Microsoft.SharePoint.WebControls.BaseFieldControl BDCFieldControl = new BDCFieldControl();

                BDCFieldControl.FieldName = InternalName;

                return BDCFieldControl;

            }

        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);
            Update();
        }

        public override void Update()
        {

            this.SetCustomProperty("Application", this.Application);
            this.SetCustomProperty("Entity", this.Entity);
            this.SetCustomProperty("DisplayFields", this.DisplayFields);
            this.SetCustomProperty("ReturnField", this.ReturnField);

            base.Update();

            if (updatedApplicationProperty.ContainsKey(ContextId))
                updatedApplicationProperty.Remove(ContextId);

            if (updatedEntityProperty.ContainsKey(ContextId))
                updatedEntityProperty.Remove(ContextId);

            if (updatedDisplayFieldsProperty.ContainsKey(ContextId))
                updatedDisplayFieldsProperty.Remove(ContextId);

            if (updatedReturnFieldProperty.ContainsKey(ContextId))
                updatedReturnFieldProperty.Remove(ContextId);

        }

    }

}
